Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

275
Vistas
right shift >> turns value into zero javascript

Trying some bit manipulation in javascript.

Consider the following:

const n = 4393751543811;
console.log(n.toString(2)) // '111111111100000000000000000000000000000011'
console.log(n & 0b11) // last two bits equal 3
const m = n >> 2; // right shift 2
// The unexpected.
console.log(m.toString(2)) // '0'

The result is 0? The expected output I am looking for after the right shift is:

111111111100000000000000000000000000000011 // pre
001111111111000000000000000000000000000000 // post >> 

How is this accomplished?

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

Javascript bitwise operators on numbers work "as if" on 32bit integers.

>> (sign-propagating right-shift for numbers) will first convert to a 32-bit integer. If you read linked spec, note specifically

Let int32bit be int modulo 232.

In other words, all bits above 32 will simply be ignored. For your number, this results in the following:

111111111100000000000000000000000000000011
┗removed━┛┗━━━━━━━━━━━━━━32bit━━━━━━━━━━━━━┛

If you want, you can use BigInt:

const n = 4393751543811n; // note the n-suffix
console.log(n.toString(2))
console.log(n & 0b11n) // for BigInt, all operands must be BigInt
const m = n >> 2n;
// The expected.
console.log(m.toString(2))

The spec for >> on BigInt uses BigInt::leftShift(x, -y), where it in turn states:

Semantics here should be equivalent to a bitwise shift, treating the BigInt as an infinite length string of binary two's complement digits.

about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda